package top.tervest.commons.sms;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.regions.ProductDomain;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;
import top.tervest.commons.sms.config.DysmsProperties;

/**
 * 短信服务类
 * @author ZhangNanNan
 * @datetime 2019-12-25 17:43
 */

@NoArgsConstructor
public class Dysms {

    private DysmsProperties dysmsProperties;

     public Dysms(DysmsProperties dysmsProperties) {
        this.dysmsProperties = dysmsProperties;
    }

    /***
     * 发送短信
     * @param tel 手机号
     * @param templateCode 短信模板Code(为空使用默认配置模板)
     * @param jsonParam 短信模板参数（json格式{"code":""123123}）
     * @param signName 短信签名
     * @return SendSmsResponse 发送短信的响应信息
     * @throws ClientException 发送短信客户的异常
     */

    public SendSmsResponse sendSms(String tel, String jsonParam, String templateCode, String signName) throws ClientException {
        DysmsProperties properties = new DysmsProperties();
        BeanUtils.copyProperties(dysmsProperties,properties);
        properties.setTemplateCode(templateCode);
        properties.setSignName(signName);
        return sendSms(tel,jsonParam,properties);
    }

    /**
     * 发送短信(使用配置模板)
     * @return SendSmsResponse 发送短信的响应信息
     * @throws ClientException 发送短信客户的异常
     */
    public SendSmsResponse sendSms(String tel, String jsonParam) throws ClientException {
        return sendSms(tel,jsonParam,dysmsProperties.getTemplateCode());
    }

    /**
     * 发送短信(自定义模板)
     * @return SendSmsResponse 发送短信的响应信息
     * @throws ClientException 发送短信客户的异常
     */
    public SendSmsResponse sendSms(String tel, String jsonParam,String templateCode) throws ClientException {
        return sendSms(tel,jsonParam,templateCode,dysmsProperties.getSignName());
    }

    /**
     * 发送短信(自定义配置)
     * @param tel 手机号
     * @param jsonParam 短信模板参数（json格式{"code":""123123}）
     * @param dysmsProperties Dysms配置类
     * @return SendSmsResponse 发送短信的响应信息
     * @throws ClientException 发送短信客户的异常
     */
    public SendSmsResponse sendSms(String tel, String jsonParam,DysmsProperties dysmsProperties) throws ClientException {
        System.setProperty("sun.net.client.defaultConnectTimeout",
                dysmsProperties.getConnectTimeout());

        System.setProperty("sun.net.client.defaultReadTimeout",
                dysmsProperties.getReadTimeout());

        IClientProfile profile = DefaultProfile.getProfile(dysmsProperties.getRegionId()
                , dysmsProperties.getAccessKeyId()
                , dysmsProperties.getAccessKeySecret());

        DefaultProfile.addEndpoint(dysmsProperties.getRegionId()
                , dysmsProperties.getProduct()
                , dysmsProperties.getEndpoint());

        DefaultAcsClient acsClient = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSysProductDomain(
                new ProductDomain(dysmsProperties.getProduct(),dysmsProperties.getDomain())
        );
        request.setSysMethod(MethodType.POST);
        request.setPhoneNumbers(tel);
        request.setSignName(dysmsProperties.getSignName());
        request.setTemplateCode(dysmsProperties.getTemplateCode());
        request.setTemplateParam(jsonParam);

        return acsClient.getAcsResponse(request);
    }
}
